
const express = require('express');
const expressStatic = require('express-static');
let queryStr = require('querystring');
let server = express();

 let users = {userName:'123'}
server.listen(8787);
server.get('/login',function(req,res){
   console.log(req.query,'1121xxxx');
   let userName = req.query['userName'];
   let psd = req.query['psd'];
   if(!userName){
       res.send({ok:false,msg:'此用户不存在'})
   }else{
       if(users[userName] != psd){
        res.send({ok:false,msg:'密码错误'})
       }else{
        res.send({ok:true,msg:'登录成功'})

       }
   }
   res.end();
})
server.post('/user',function(req,res){
    let str = '';
    req.on('data',(data)=>{//如前端的事件
        str += data;
    }); 
    //end 数据全部结束
    req.on('end',()=>{//如前端的事件
        console.log(queryStr.parse(str),"queryStr.parse(str)")
        let userName = queryStr.parse(str)['userName'];
        let psd = queryStr.parse(str)['psd'];
        if(!userName){
            res.send({ok:false,msg:'此用户不存在'})
        }else{
            if(users[userName] != psd){
            res.send({ok:false,msg:'密码错误'})
            }else{
            res.send({ok:true,msg:'登录成功'})
    
            }
        }
        res.end();
    }); 
 
 })

server.use(expressStatic('./www'));